<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商铺标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入商铺标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商铺类型" prop="storeType">
        <el-select v-model="queryParams.storeType" placeholder="请选择商铺类型">
          <el-option label="全部" value=""></el-option>
          <el-option v-for="(item, index) in storeTypeList" :key="index" :label="item.title"
            :value="item.id"></el-option>
        </el-select>
        <!-- <el-input v-model="queryParams.storeType" placeholder="请输入商铺类型" clearable @keyup.enter.native="handleQuery" /> -->
      </el-form-item>
      <el-form-item label="营业时间" prop="yieTime">
        <el-input v-model="queryParams.yieTime" placeholder="请输入营业时间" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="商铺地址" prop="storeAddr">
        <el-input v-model="queryParams.storeAddr" placeholder="请输入商铺地址" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="筛选时间" prop="storeAddr">
        <el-date-picker v-model="filterDate" type="daterange" value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始日期"
          end-placeholder="结束日期" @change="handleChangeDate">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:stores:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:stores:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:stores:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:stores:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="商铺标题" align="center" prop="title" />
      <el-table-column label="商铺LOGO" align="center" prop="storeIcon" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.storeIcon" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="商铺类型" align="center" prop="storeType">
        <template slot-scope="scope">{{ transformStoreName(scope.row.storeType) }}</template>
      </el-table-column>
      <el-table-column label="导航次数" sortable align="center" prop="browseNum" />
      <el-table-column label="联系次数" sortable align="center" prop="linkNum" />
      <el-table-column label="营业时间" align="center" prop="yieTime" />
      <el-table-column label="商铺电话" align="center" prop="storePhone" />
      <el-table-column label="商铺地址" align="center" prop="storeAddr" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:stores:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:stores:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改商铺管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="商铺标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入商铺标题" />
        </el-form-item>
        <el-form-item label="商铺LOGO" prop="storeIcon">
          <image-upload v-model="form.storeIcon" />
        </el-form-item>
        <el-form-item label="商铺类型" prop="storeType">
          <el-select v-model="form.storeType" placeholder="请选择商铺类型">
            <el-option v-for="(item, index) in storeTypeList" :key="index" :label="item.title"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="商铺简介" prop="storeBlurb">
          <el-input type="textarea" v-model="form.storeBlurb" placeholder="请输入商铺简介" />
        </el-form-item>
        <el-form-item label="营业时间" prop="yieTime">
          <el-input v-model="form.yieTime" placeholder="请输入营业时间" />
        </el-form-item>
        <el-form-item label="商铺电话" prop="storePhone">
          <el-input v-model="form.storePhone" placeholder="请输入商铺电话" />
        </el-form-item>
        <el-form-item label="商铺地址" prop="storeAddr">
          <el-input v-model="form.storeAddr" placeholder="请输入商铺地址" />
        </el-form-item>
        <el-form-item label="经纬度" class="is-required" prop="longitudeAndLatitude">
          <el-input v-model.trim="longitudeAndLatitude" placeholder="请输入经纬度" />
          <el-button class="mt10" type="primary" @click="openDialog = true">选择经纬度</el-button>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 经纬度 -->
    <el-dialog title="获取经纬度" :visible.sync="openDialog" append-to-body width="80%">
      <iframe :src="'https://lbs.qq.com/getPoint/'" width="100%" height="750px" frameBorder="0"></iframe>
    </el-dialog>
  </div>
</template>

<script>
import { listStoresType } from '@/api/system/storesType'
import { listStores, getStores, delStores, addStores, updateStores } from "@/api/system/stores";

export default {
  name: "Stores",
  data() {
    var validateLongitude = (rule, value, callback) => {
      if (!this.longitudeAndLatitude) {
        callback(new Error('请选择经纬度'))
      } else {
        callback();
      }
    }
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商铺管理表格数据
      storesList: [],
      storeTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDialog: false,
      filterDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        storeIcon: null,
        storeType: null,
        storeBlurb: null,
        yieTime: null,
        storeAddr: null,
        longitude: null,
        latitude: null,
        beginDate: null,
        endDate: null,
      },
      longitudeAndLatitude: '',
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "商铺标题不能为空", trigger: "blur" }
        ],
        storeIcon: [
          { required: true, message: "商铺LOGO不能为空", trigger: "blur" }
        ],
        storeType: [
          { required: true, message: "商铺类型不能为空", trigger: "blur" }
        ],
        storeBlurb: [
          { required: true, message: "商铺简介不能为空", trigger: "blur" }
        ],
        yieTime: [
          { required: true, message: "营业时间不能为空", trigger: "blur" }
        ],
        storePhone: [
          { required: true, message: "商铺电话不能为空", trigger: "blur" }
        ],
        storeAddr: [
          { required: true, message: "商铺地址不能为空", trigger: "blur" }
        ],
        longitudeAndLatitude: [
          { validator: validateLongitude, trigger: ['change', 'blur'] }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getStoreTypes();
  },
  methods: {
    handleChangeDate(arr) {
      this.queryParams.beginDate = arr[0]
      this.queryParams.endDate = arr[1]
    },
    /** 查询商铺管理列表 */
    getList() {
      this.loading = true;
      listStores(this.queryParams).then(response => {
        this.storesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        storeIcon: null,
        storeType: null,
        storeBlurb: null,
        yieTime: null,
        storePhone: null,
        storeAddr: null,
        longitude: null,
        latitude: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.filterDate = []
      this.queryParams.beginDate = null
      this.queryParams.endDate = null
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商铺管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStores(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商铺管理";
        this.longitudeAndLatitude = this.form.latitude + ',' + this.form.longitude
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.latitude = this.longitudeAndLatitude.split(",")[0]
          this.form.longitude = this.longitudeAndLatitude.split(",")[1]
          if (this.form.id != null) {
            updateStores(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {

            addStores(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商铺管理编号为"' + ids + '"的数据项？').then(function () {
        return delStores(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/stores/export', {
        ...this.queryParams
      }, `stores_${new Date().getTime()}.xlsx`)
    },
    // 商铺类型
    getStoreTypes() {
      listStoresType({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        this.storeTypeList = res.rows;
      })
    },
    // 商铺类型转换
    transformStoreName(id) {
      return this.storeTypeList.filter(item => item.id === id)[0].title
    }
  },

};
</script>
